package com.thinksns.sociax.t4.android.verify;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.thinksns.sociax.android.R;
import com.thinksns.sociax.constant.Verify;
import com.thinksns.sociax.t4.android.img.Bimp;
import com.thinksns.sociax.t4.model.ModelAttach;
import com.thinksns.sociax.t4.unit.UnitSociax;
import com.thinksns.sociax.thinksnsbase.base.ListBaseAdapter;
import com.thinksns.sociax.thinksnsbase.bean.SociaxItem;

import java.util.List;

import static com.thinksns.sociax.api.Api.mContext;

/**
 * @author LeiYan
 * @describe
 * @date 2017/7/26
 * @contact email:907238552@qq.com
 */

public class VerifyAttachAdapter extends BaseAdapter {
    List<ModelAttach> mData;
    private Context mContext;
    private int mWidth;
    private LayoutInflater mLayoutInflater;
    private GridView mGridView;
    private boolean needPlaceHolder; // 最后一张需要占位图
    private int mMaxCount;

    public VerifyAttachAdapter(Context context, GridView gridView) {
        this(context, gridView, true, Verify.MAX_ATTACH_COUNT);
    }

    public VerifyAttachAdapter(Context context, GridView gridView, boolean needPlaceHolder, int maxCount) {
        this.mContext = context;
        this.needPlaceHolder = needPlaceHolder;
        mGridView = gridView;
        mWidth = UnitSociax.dip2px(context, 60);
        mLayoutInflater = LayoutInflater.from(mContext);
        mMaxCount = maxCount;
    }

    public void setData(List<ModelAttach> data) {
        this.mData = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        int count;
        if (!needPlaceHolder) {
            count = mData == null ? 0 : mData.size();
        } else {
            count = mData == null ? 0 : mData.size() >= mMaxCount ? mMaxCount : mData.size() + 1;
        }
        //计算列表的宽度
        int gridviewWidth = count * mWidth + (count - 1) * 20;
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                gridviewWidth, mWidth);
        mGridView.setLayoutParams(params);         // 重点
        mGridView.setNumColumns(count);
        return count;
    }

    @Override
    public ModelAttach getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.item_published_grida, parent, false);
            imageView = (ImageView) convertView.findViewById(R.id.item_grida_image);
            imageView.setLayoutParams(new LinearLayout.LayoutParams(mWidth, mWidth));
            convertView.setTag(imageView);
        } else {
            imageView = (ImageView) convertView.getTag();
        }
        if (position == mData.size()) {
            imageView.setImageResource(R.drawable.icon_addpic_unfocused);
        } else
            UnitSociax.loadImageNoAnim(mContext, imageView, getItem(position).getImage_url(), R.drawable.image120x120);
        return convertView;
    }

}
